package com.qn.service;

import com.qn.grpc.protobuf.news.News;
import com.qn.grpc.protobuf.news.NewsRequest;
import com.qn.grpc.protobuf.news.NewsResponse;
import com.qn.grpc.protobuf.news.NewsServiceGrpc.NewsServiceImplBase;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

import java.util.ArrayList;
import java.util.List;

/**
 * grpc服务器服务
 *
 * @author qn
 * @date 2023/03/17
 */
@GrpcService
@Slf4j
public class GrpcServerService extends NewsServiceImplBase {

    @Override
    public void list(NewsRequest request, StreamObserver<NewsResponse> responseObserver) {
        List<News> newsList = new ArrayList<>();
        newsList.add(News.newBuilder()
                .setId(1)
                .setTitle("新闻1")
                .setContent("内容1")
                .build());
        newsList.add(News.newBuilder()
                .setId(2)
                .setTitle("新闻2")
                .setContent("内容2")
                .build());

        NewsResponse response = NewsResponse.newBuilder().addAllNews(newsList).build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
        super.list(request, responseObserver);
    }
}

